<?php

declare(strict_types=1);

namespace App\AdminApi\Modular\Administer\Service;

use App\Common\Modular\Administer\Enums\UserEnums;
use App\Common\Modular\Administer\Model\UserModel;
use App\Common\Modular\Administer\Serve\JobServe;
use App\Common\Modular\Administer\Serve\RoleServe;
use App\Common\Modular\Administer\Serve\UserServe;
use Zms\Exception\AppException;
use Core\Package\ElementPlus\FormCreate\Form;
use Core\Package\ElementPlus\FormCreate\FormItem;
use Zms\Exception\FormException;
use Zms\Unit\Lists;

class UserService extends UserServe
{
    /**
     * 创建管理员表单
     * @return Form
     * @throws FormException
     */
    public function createdUserForm(): Form
    {
        $rule[] = FormItem::Input('nickname', '管理员昵称')->clearable(true)->maxlength(20)->required('请输入管理员昵称');
        $rule[] = FormItem::Input('account', '登录账号')->placeholder('后台登录账号')->clearable(true)->required('请输入后台登录账号')->maxlength(30);
        $rule[] = FormItem::Input('password', '登录密码')->placeholder('后台登录密码')->maxlength(30)->required('请输入登录密码')->clearable(true);
        $rule[] = FormItem::ImageInput('avatar', '账号头像')->required('请设置账号头像')->clearable(true);
        $rule[] = FormItem::Cascader('department_id', '所属部门')->options(container(DepartmentService::class)->getTreeFormData())->checkStrictly(true)->valueKey('id')->labelKey('name')->clearable(true);
        $rule[] = FormItem::Select('job_id', '所在职位')->options(container(JobServe::class)->JobFormData())->clearable(true);
        $rule[] = FormItem::Switchs('is_super', '超级权限', (string)UserEnums::SUPER_NO)->hint('选择后此管理员将拥有所有操作权限');
        $rule[] = FormItem::SelectMuliple('role_id', '权限角色')->required('请为此管理员分配角色')->multipleLimit(3)->filterable(false)->clearable(true)->options(container(RoleServe::class)->roleFormData())->hint('最多可选三个角色');
        $rule[] = FormItem::Radio('data_auth_type', '数据权限', UserEnums::DATA_AUTH_TYPE_SELF)->options(UserEnums::Datas(UserEnums::TAG['DATA_AUTH_TYPE']))->required('请给管理员分配数据权限')->control([
            [
                'value' => UserEnums::DATA_AUTH_TYPE_DEP,
                'rule' => [
                    FormItem::Cascader('auth_dep', '授权部门')->options(container(DepartmentService::class)->getTreeFormData())->checkStrictly(true)->valueKey('id')->multiple(true)->labelKey('name')->clearable(true)->required('请选择部门')->hint('查看本人和指定部门的数据权限')
                ]
            ]
        ])->hint("超级管理员或超级权限拥有所有数据权限,【本人】仅能查看操作本人的数据,【本部门】可查看当前部门权限,当账号为部门负责人时可同时查看下级部门数据,【指定部门】查看本人和指定部门的数据权限,【我管理的部门】查看所有我管理的部门和下级部门");
        return Form::create('添加管理员')->setRules($rule)->setOptionFormInline(true);
    }


    /**
     * 编辑管理员表单
     * @param string $id
     * @return Form
     * @throws AppException
     */
    public function updatedUserForm(string $id = ''): Form
    {
        /**
         * @var UserModel $user
         */
        $user = $this->search(['id' => $id])->first();
        if ($id && $user) {
            $dep = container(DepartmentService::class)->getArchitectureIds($user->department_id);
            $rule[] = FormItem::Hidden('id', $id);
            $rule[] = FormItem::Input('nickname', '管理员昵称', $user->nickname)->clearable(true)->maxlength(20)->required('请输入管理员昵称');
            $rule[] = FormItem::Input('account', '登录账号', $user->account)->placeholder('后台登录账号')->clearable(true)->required('请输入后台登录账号')->maxlength(30);
            $rule[] = FormItem::Input('password', '登录密码')->placeholder('后台登录密码')->maxlength(30)->hint('不填写则代表不更改')->clearable(true);
            $rule[] = FormItem::ImageInput('avatar', '账号头像', $user->avatar)->required('请设置账号头像')->clearable(true);
            $rule[] = FormItem::Cascader('department_id', '所属部门', Lists::toStringList($dep))->options(container(DepartmentService::class)->getTreeFormData())->checkStrictly(true)->valueKey('id')->labelKey('name')->clearable(true);
            $rule[] = FormItem::Select('job_id', '所在职位', strval($user->job_id ? $user->job_id : ''))->options(container(JobServe::class)->JobFormData())->clearable(true);
            if ($user->id !== 1) {
                $rule[] = FormItem::Switchs('is_super', '超级权限', $user->is_super ? '1' : '0')->hint('选择后此管理员将拥有所有操作权限');
                $rule[] = FormItem::SelectMuliple('role_id', '权限角色', Lists::toStringList($user->role_id))->required('请为此管理员分配角色')->multipleLimit(3)->filterable(false)->clearable(true)->options(container(RoleServe::class)->roleFormData())->hint('最多可选三个角色');
                $rule[] = FormItem::Radio('data_auth_type', '数据权限', $user->data_auth_type)
                    ->options(UserEnums::Datas(UserEnums::TAG['DATA_AUTH_TYPE']))
                    ->required('请给管理员分配数据权限')
                    ->control([
                        [
                            'value' => UserEnums::DATA_AUTH_TYPE_DEP,
                            'rule' => [
                                FormItem::Cascader('auth_dep', '授权部门', $user->auth_dep)->options(container(DepartmentService::class)->getTreeFormData())->checkStrictly(true)->valueKey('id')->multiple(true)->labelKey('name')->clearable(true)->required('请选择部门')->hint('查看本人和指定部门的数据权限')
                            ]
                        ]
                    ])
                    ->hint("超级管理员或超级权限拥有所有数据权限,【本人】仅能查看操作本人的数据,【本部门】可查看当前部门权限,当账号为部门负责人时可同时查看下级部门数据,【指定部门】查看本人和指定部门的数据权限,【我管理的部门】查看所有我管理的部门和下级部门");
            } else {
                $rule[] = FormItem::Hidden('role_id', ['*']);
            }
            return Form::create('编辑管理员')->setRules($rule)->setOptionFormInline(true);
        }
        throw new AppException('帐号不存在或已删除');
    }

}